package org.adream.account.service;

import org.adream.account.aop.annotation.DataSource;
import org.adream.account.dao.Dream2020FeedDao;
import org.adream.account.dao.Dream2020UserDao;
import org.adream.account.entity.Dream2020FeedEntity;
import org.adream.account.entity.Dream2020UserEntity;
import org.adream.account.model.ResultModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 新版盒子用户service
 * @author sl
 *
 */
@Service
@DataSource("adreambox2020DataSource")
public class Dream2020FeedService {
	
	@Autowired
	private Dream2020UserDao dream2020UserDao;
	
	@Autowired
	private Dream2020FeedDao dream2020FeedDao;
	
	private final static Logger logger = LoggerFactory.getLogger(Dream2020FeedService.class);
	
	/**
	 * 添加用户动态
	 * @param dream2020Feed
	 * @return
	 */
	public ResultModel<String> addDream2020Feed(Dream2020FeedEntity dream2020Feed) {
		//根据casid查询user_id
		Dream2020UserEntity dream2020User = dream2020UserDao.queryDream2020UserByCasId(dream2020Feed.getCasid());
		if(dream2020User == null || dream2020Feed.getDeletedAt() != null) {
			logger.warn("添加动态失败,用户不存在或已被删除,casid:{}", dream2020Feed.getCasid());
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "添加动态失败,用户不存在或已被删除", null);
		}
		int userId = dream2020User.getId();
		dream2020Feed.setUserId(userId);
		//唯一标记：uid+13位的时间戳或13位的时间戳+uid
		dream2020Feed.setFeedMark(Long.valueOf(userId + "" + System.currentTimeMillis()));
		int result = dream2020FeedDao.addDream2020Feed(dream2020Feed);
		if(result == 0) {
			logger.warn("添加动态失败,数据库错误,userId:{},feedContent:{}", userId, dream2020Feed.getFeedContent());
			return new ResultModel<String>(ResultModel.ERR_DATABASE, "系统错误321,请联系管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, "操作成功", null);
	}
}